<?php
namespace app\api\controller;

use \think\Controller;
use \think\Db;
class Friends extends Common
{
    /**
     *
     * user_id
     * @return string
     */
    public function index(){
        $uid = $_POST['user_id'];
        $data = array();//状态0代表没有新信息，1代表有新消息
        $data['notice'] = '';
        $data['dog'] = '';
        $data['message'] = '';
        $data['friend'] = '';
        $notice = Db::table('up_notice')->order('n_createtime DESC')->find();
        if($notice){
            $data['notice'] = $notice['n_content'];
            $data['notice_time'] = $notice['n_createtime'];
        }
        $dogs = Db::table('up_u_user_interest_dog')
            ->where('user_id',$uid)
            ->select();
        if($dogs){
            $myguanzhu = array();
            foreach ($dogs as $g){
                $myguanzhu[] = $g['udf_id'];
            }
            //默认是四种狗的所有状态
            $dog = Db::table('up_dog')->alias('d')
                ->join('up_u_user_interest_dog ud','d.udf_id=ud.udf_id','LEFT')
                ->where('d.udf_id','in',$myguanzhu)//我关注的,d
                ->where('dog_preserve','2')//不是暂存的
                ->where('`dog_release_time` > `ud_datetime`')//关注日期之后的
                ->order('dog_release_time DESC')
                ->find();
            if($dog){
                $data['dog'] = $dog['dog_information'];
                $data['dog_time'] = $dog['dog_release_time'];
                if(!$data['dog']){$data['dog'] = '';}
            }
        }

        //===============
        $message = Db::table('up_message')
            ->where(['m_reply_id'=>$uid,'m_status_reply'=>1])//,'m_status_reply'=>array('>',0)//
            ->whereOr(['m_publish_id'=>$uid])//,'m_status'=>0
//            ->order('m_reply_datetime,m_datetime DESC')
            ->order('m_datetime DESC')
            ->find();
        if($message){
            if($message['m_publish_id'] == $uid){
                $data['message'] = $message['m_message'];
                $data['message_time'] = $message['m_datetime'];
            }else{
                $data['message'] = $message['m_message_reply'];
                $data['message_time'] = $message['m_reply_datetime'];
            }
        }
        //================
        //================
        $users = Db::table('up_u_user_interest_user')
            ->where('uu_userid',$uid)
            ->select();
        if($users){
            $myguanzhu = array();
            foreach ($users as $g){
                $myguanzhu[] = $g['uu_attention_userid'];
            }
            //关注用户状态
            $dog = Db::table('up_dog')->alias('d')
                ->join('up_u_user_interest_user uu','d.user_id=uu.uu_attention_userid','LEFT')
                ->where('d.user_id','in',$myguanzhu)//我关注的
                ->where('dog_preserve','2')//不是暂存的
                ->where('`dog_release_time` > `uu_datetime`')
                ->order('dog_release_time DESC')
                ->find();
            if($dog){
                $data['friend'] = $dog['dog_information'];
                $data['friend_time'] = $dog['dog_release_time'];
                if(!$data['friend']){$data['friend'] = '';}
            }
        }
        //==========================
         return json_encode(['status'=>200,'msg'=>$data]);
    }
//    function index(){
//        $uid = $_POST['user_id'];
//        $data = array();//状态0代表没有新信息，1代表有新消息
//        $data['notice'] = '';
//        $data['notice_status'] = 1;
//        $data['message_status'] = 0;
//        $data['friend'] = '';
//        $data['friend_status'] = 1;
//        $data['dog'] = '';
//        $data['dog_status'] = 1;
//        $notice = Db::table('up_notice')->order('n_createtime DESC')->find();
//        if($notice){
//            $data['notice'] = $notice;
//            $nids = trim($notice['n_dis'],',');
//            $arr_ndis = explode(',',$nids);
//            if($arr_ndis){
//                if(in_array($uid,$arr_ndis)){
//                    $data['notice_status'] = 0;
//                }
//            }
//        }
//        //==================================
//        $message1 = Db::table('up_message')
//            ->where('m_publish_id',$uid)
//            ->where('m_status',0)
//            ->find();
//        $message2 = Db::table('up_message')
//            ->where('m_reply_id',$uid)
//            ->where('m_status',1)
//            ->find();
//        if($message1 or $message2){
//            $data['message_status'] = 1;
//        }
//        $users = Db::table('up_u_user_interest_user')
//            ->where('uu_userid',$uid)
//            ->select();
//        if($users){
//
//        }
//        return json_encode(['status'=>200,'msg'=>$data]);
//    }
//

    /**
     * @return string
     * type,user_id,page
     * type:1通知，2留言，3伙伴动态，4狗狗动态
     */
    public function getList(){
        $uid = $_POST['user_id'];
        $type = $_POST['type'];
        $page = $_POST['page'];
        $datas = array();
        if($type == 1){
            $time = Db::table('up_u_user')->where('user_id',$uid)->value('user_time');
            $datas = Db::table('up_notice')->where('n_createtime','>=',"$time")
                ->order('n_createtime DESC')
                ->limit(($page-1)*10,10)->select();
        }
        if($type == 2){
//            return json_encode(['status'=>200,'msg'=>11]);
            $datas = Db::table('up_message')->alias('m')
                ->join('up_u_user u','u.user_id=m.m_publish_id','LEFT')
                ->join('up_u_user u2','u2.user_id=m.m_reply_id','LEFT')
                ->where(['m_reply_id'=>$uid,'m_status_reply'=>1])//,'m_status_reply'=>array('>',0)//
                ->whereOr(['m_publish_id'=>$uid])//,'m_status'=>0
                ->order('m_datetime DESC')
                ->field('m_id,m_dog_type,m_publish_id,m_reply_id,m_dog_id,m_message,m_message_reply,m_reply_datetime,m_datetime,u.user_username username,u2.user_username reply_username,u.user_image,u2.user_image reply_userimage')
                ->limit(($page-1)*10,10)->select();
//            foreach ($datas as $k=>$v){
//
//            }
//            return json_encode(['status'=>200,'msg'=>$datas]);
//            if($datas){
//                foreach ($datas as $k=>$v){
////                    //===如果我是留言者，没有人回复的话就不显示了.
////                    if($v['m_reply_id'] == $uid){
////                        if($v['m_message_reply'] == ""){//通过状态去判断
////                            unset($datas[$k]);
////                        }
////                    }
////                    //==end==
//                    $ly_date = $v['m_reply_datetime'];
//                    $date = date('Y-m-d').' 00:00:00';
//                    if($ly_date < $date){
//                        $ly_time = strtotime($v['m_reply_datetime']);
//                        $time = time();
//                        $days = ceil(($time-$ly_time)/3600/24);
//                        if($days<=15){
//                            $datas[$k]['day_ago'] = $days . '天前';
//                        }else{
//                            $datas[$k]['day_ago'] = '更早';
//                        }
//                    }else{
//                        $datas[$k]['day_ago'] = substr($v['m_reply_datetime'],11,5);
//                    }
//
//                }
//            }

        }
        if($type == 3){
            $users = Db::table('up_u_user_interest_user')
                ->where('uu_userid',$uid)
                ->select();
            if($users){
                $myguanzhu = array();
                foreach ($users as $u){
                    $myguanzhu[] = $u['uu_attention_userid'];
                }
                //hb状态
                if($page >= 5){$page =5;}
                $datas = Db::table('up_dog')->alias('d')
                    ->join('up_u_user u','u.user_id=d.user_id','LEFT')
                    ->join('up_u_user_interest_user uu','d.user_id=uu.uu_attention_userid','LEFT')
                    ->where('d.user_id','in',$myguanzhu)//我关注的
                    ->where('dog_preserve','2')//不是暂存的
                    ->where('`dog_release_time` > `uu_datetime`')//关注日期之后的
                    ->order('dog_release_time DESC')
                    ->field('d.*,u.user_username')
                    ->limit(($page-1)*10,10)->select();
            }
        }
        if($type == 4){
            $dogs = Db::table('up_u_user_interest_dog')
                ->where('user_id',$uid)
                ->select();
            if($dogs){
                $myguanzhu = array();
                foreach ($dogs as $g){
                    $myguanzhu[] = $g['udf_id'];
                }
                //默认是四种狗的所有状态
                if($page >= 5){$page =5;}
                $datas = Db::table('up_dog')->alias('d')
                    ->join('up_dog_family f','d.udf_id=f.udf_id','LEFT')
                    ->join('up_u_user_interest_dog ud','d.udf_id=ud.udf_id','LEFT')
                    ->where('d.udf_id','in',$myguanzhu)//我关注的
                    ->where('dog_preserve','2')//不是暂存的
                    ->where('`dog_release_time` > `ud_datetime`')//关注日期之后的
                    ->order('dog_release_time DESC')
                    ->field('d.*,f.udf_name')
                    ->limit(($page-1)*10,10)->select();
            }
        }
        return json_encode(['status'=>200,'msg'=>$datas]);
    }
    /**
     * @return string
     * id(数据id)
     *
     */
    public function getNoticeInfo(){
            $nid = $_POST['id'];
            $data = Db::table('up_notice')->where('n_id',$nid)
                ->find();
        return json_encode(['status'=>200,'msg'=>$data]);
    }
    /**
     * 获得留言数据：从列表出来的
     * id（狗的id），page
     *返回： msg：进展数据
     */
    public function getLyInfo(){
        $did = $_POST['id'];
        $page = $_POST['page'];
            $data = Db::table('up_message')->alias('m')
                ->join('up_u_user u','u.user_id=m.m_reply_id','LEFT')
                ->where('m_dog_id',$did)
                ->order('m_reply_datetime,m_datetime DESC')
                ->limit(($page-1)*10,10)->select();
        return json_encode(['status'=>200,'msg'=>$data]);
    }
    /**
     * 获得进展数据：从列表出来的
     * id（狗的id），page
     *返回： msg：进展数据
     */
    public function getJzInfo(){
        extract(input());
        $did = $_POST['id'];
        $page = $_POST['page'];
            $data = Db::table('up_jinzhan')->where('j_dog_id',$did)
                ->order('j_datetime DESC')
                ->limit(($page-1)*10,11)->select();
            $many = count($data);
            if($many >= 11){
                $is = 1;
                unset($data[10]);
            }else{
                $is = 0;
            }
            if($data){
                foreach ($data as $k=>$v){
                    $data[$k]['zan'] = 0;
                    $img = trim($v['j_img'],',');
                    if($img){
                        $data[$k]['j_img'] = explode(',',$img);
                    }
                    $zan = Db::table('up_jinzhan_user')
                        ->where('ju_user_id',$user_id)->where('ju_j_id',$v['j_id'])
                        ->find();
                    if($zan){
                        $data[$k]['zan'] = 1;
                    }
                }
            }
        $who = Db::table('up_dog')->where('dog_id',$did)->find()['user_id'];
        return json_encode(['status'=>200,'msg'=>$data,'last'=>$is,'who'=>$who]);
    }
    /**
     * 获取用户详情
     * uid，user_id
     *返回： msg：用户信息，is：是否已经关注
     */
    public function getUserInfo(){
        $uid = $_POST['uid'];
        $userid = $_POST['user_id'];
            $data = Db::table('up_u_user')->alias('u')
                ->join('up_area u1','u.user_province=u1.district_id','left')
                ->join('up_area ua','u.user_city=ua.district_id','left')
                ->join('up_area uaa','u.user_area=uaa.district_id','left')
                ->where('user_id',$uid)
                ->field('u.*,ua.district shi,uaa.district qu')
                ->find();
            $guanzhu = Db::table('up_u_user_interest_user')
                ->where('uu_userid',$userid)
                ->where('uu_attention_userid',$uid)
               ->find();
            if($guanzhu){
                $is = 1;
            }else{
                $is = 0;
            }
        return json_encode(['status'=>200,'msg'=>$data,'is'=>$is]);
    }

    /**
     * 此用户发的动态
     * uid，page
     *返回： msg：用户信息，is：是否已经关注
     */
    public function getHomeDateForHuoban(){
        $userid = $_POST['uid'];
        $page = $_POST['page'];
        $where1 = [
            'dog_preserve'=>2,
            'ud.user_id'=>$userid
        ];
        $data = $this->getHomePageData($where1,$page);
        return json_encode(['status'=>200,'msg'=>$data]);
    }

    /**
     * 关注或者取消关注
     * id，user_id
     * @return string
     */
    public function guanzhuOrNotUser(){
        $id = $_POST['uid'];
        $uid = $_POST['user_id'];
        $pd = Db::table('up_u_user_interest_user')->where(['uu_userid'=>$uid,'uu_attention_userid'=>$id])->find();
        if($pd){
            $res = Db::table('up_u_user_interest_user')->where(['uu_userid'=>$uid,'uu_attention_userid'=>$id])->delete();
        }else{
            $res = Db::table('up_u_user_interest_user')->insert(['uu_userid'=>$uid,'uu_attention_userid'=>$id,'uu_datetime'=>date('Y-m-d H:i:h')]);
        }
        if($res){
            return json_encode(['status'=>200,'msg'=>'操作成功']);
        }else{
            return json_encode(['status'=>0,'msg'=>'操作失败']);
        }
    }


    /**
     * 给老汤的，聊天用
     */
    public function byIdsGetInfos()
    {
        $uids = trim($_POST['uids'],',');
        $pd = Db::table('up_u_user')
            ->where('user_id','in',$uids)
            ->field('user_username,user_id,user_image')
            ->select();
        return json_encode(['status'=>200,'msg'=>$pd]);
    }

}